package ncme.auto.framework.core.services.Impl.mian;

import ncme.auto.framework.core.callback.AutoProcessControllerPart;
import ncme.auto.framework.core.entities.aspect.parameter.ServiceInfo;
import ncme.auto.framework.core.services.Impl.base.BaseAutoMapperServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjh
 * @Description
 * @create 2022/2/14 9:52
 */
public abstract class ManyToManyAutoMapperImpl<E, PRIMARY_CLASS, SECONDARY_CLASS> extends BaseAutoMapperServiceImpl<E, PRIMARY_CLASS, SECONDARY_CLASS> {

    public abstract void customService(AutoProcessControllerPart autoProcessControllerPart, ServiceInfo serviceInfo) throws Throwable;

    //region######################################## get ###########################################

    public List<Integer> getSecondaryIdsFromPrimaryId(Integer primaryId) {
        return getIdsFromId(primaryId, true);
    }

    public List<SECONDARY_CLASS> getSecondaryEntitiesFromPrimaryId(Integer primaryId) {
        List<SECONDARY_CLASS> secondaryEntities = new ArrayList<>();
        List<Integer> secondaryIds = getSecondaryIdsFromPrimaryId(primaryId);
        secondaryIds.forEach(secondaryId -> secondaryEntities.add(secondaryEntityBaseService.getEntityById(secondaryId, true)));
        return secondaryEntities;
    }

    public List<Integer> getPrimaryIdsFromSecondaryId(Integer secondaryId) {
        return getIdsFromId(secondaryId, false);
    }

    public List<PRIMARY_CLASS> getPrimaryEntitiesFromSecondaryId(Integer secondaryId) {
        List<PRIMARY_CLASS> primaryEntities = new ArrayList<>();
        List<Integer> primaryIds = getPrimaryIdsFromSecondaryId(secondaryId);
        primaryIds.forEach(primaryId -> primaryEntities.add(primaryEntityBaseService.getEntityById(primaryId, true)));
        return primaryEntities;
    }

    //endregion

    //region######################################## add ###########################################

    public void addSecondaryIdsForPrimaryId(Integer primaryId, List<Integer> secondaryIds) {
        addMappers(primaryId, secondaryIds, true);
    }

    public void addPrimaryIdsForSecondaryId(Integer secondaryId, List<Integer> primaryIds) {
        addMappers(secondaryId, primaryIds, false);
    }

    //endregion

    //region######################################## edit #########################################

    public void editSecondaryIdsOfPrimaryId(Integer primaryId, List<Integer> secondaryIds) {
        editMappers(primaryId, secondaryIds, true);
    }

    public void editPrimaryIdsOfSecondaryId(Integer secondaryId, List<Integer> primaryIds) {
        editMappers(secondaryId, primaryIds, false);
    }

    //endregion

    //region######################################## delete ###########################################

    public void deleteSecondaryIdsOfPrimaryId(Integer primaryId, List<Integer> secondaryIds) {
        deleteMappers(primaryId, secondaryIds, true);
    }

    public void deletePrimaryIdsOfSecondaryId(Integer secondaryId, List<Integer> primaryIds) {
        deleteMappers(secondaryId, primaryIds, false);
    }

    //endregion

}
